package com.edu.test.idTest;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 

import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * -代码工具类，通用
 * @author sixmonth
 * @Date 2019年5月23日
 *
 */
public class IDUtil {
 
		/**
		 * -获取带毫秒时间，通用，格式化时间："yyyy-MM-dd HH:mm:ss"
		 * @return
		 */
		public static String getyyyyMMddHHmmss(){
//			Date d = new Date();
//	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//			return sdf.format(d);
			DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
			return dtf.format(LocalDateTime.now());
		}

		/**
		 * -获取带毫秒时间戳，格式化时间：“yyyyMMddHHmmssSSS”
		 * @return
		 */
		public static String getyyyyMMddHHmmssSSS(){
			DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
			return dtf.format(LocalDateTime.now());
		}
		
		/**
		 * -获取当前日期，格式化时间："yyyyMMdd"
		 * @return
		 */
		public static String getyyyyMMdd(){
			DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
			return dtf.format(LocalDateTime.now());
		}
		
		/**
		 * -获取当前日期，格式化："yyyy-MM-dd"
		 * @return
		 */
		public static String getyyyy_MM_dd(){
			DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
			return dtf.format(LocalDateTime.now());
		}
		
		/**
		 * -获取前一天时间
		 * @return
		 */
		public static String getq_yyyy_MM_dd(){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			long l=System.currentTimeMillis()-1000*60*60*24;
			return sdf.format(l);
		}
		
		/**
		 * -获取前一月时间
		 * @return
		 */
		public static String getqy_yyyy_MM_dd(){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			long l=System.currentTimeMillis()-1000*60*60*24*30;
			return sdf.format(l);
		}
		
		/**
		 * -获取10000-100000的随机数
		 * @return
		 */
		public static int getRandom(){
			int max=100000;
	        int min=10000;
	        Random random = new Random();
	        int s = random.nextInt(max)%(max-min+1) + min;
	        return s;
		}
		
		
		/**
		 * -获取服务器IP地址
		 * @param request
		 * @return
		 */
		public  static String getIp(HttpServletRequest request) {
			String ip = request.getHeader("X-Pounded-For");
			if (ip != null) {
				return ip;
			}
 
			ip = request.getHeader("x-forwarded-for");
 
			if (ip == null) {
				ip = request.getRemoteAddr();
				if (ip.equals("127.0.0.1") || ip.equals("0:0:0:0:0:0:0:1")) {
					InetAddress address;
					try {
						address = InetAddress.getLocalHost();
						ip = address.getHostAddress();
					} catch (UnknownHostException e) {
						e.printStackTrace();
					}
 
				}
				return ip;
			} else {
				StringTokenizer tokenizer = new StringTokenizer(ip, ",");
				for (int i = 0; i < tokenizer.countTokens() - 1; i++) {
					tokenizer.nextElement();
				}
 
				ip = tokenizer.nextToken().trim();
 
				if (ip.equals("")) {
					ip = null;
				}
			}
 
			if (ip == null) {
				ip = "0.0.0.0";
			}
 
			return ip;
		}
		
		/**
		 * -获取uuid
		 * @return
		 */
		public static String getUUID()
		  {
		    String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		    return uuid;
		  }
		
		/**
		 *- 获取10位整数随机数
		 * @return
		 */
		public static Integer getIntUUID() {
			Integer num = Math.abs((int) Math.round((Math.random()*10+1)*1000000000));
			return num;
		}
		
		/**
		 * -获取简版UUID，年月日时分秒时间戳+8位uuid
		 * @return
		 */
		public static String getSimpleUUID() {
			UUID uuid = UUID.randomUUID();
		    String sdf = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		    String uuidNum = uuid.toString().substring(0, 8);
		    uuidNum = sdf + uuidNum;
		    return uuidNum ;
		}
		
		/**
		 * -获取年月日时间戳+8位随机数uuid
		 * @return
		 */
		public static String getDateRandomUUID() {
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			Integer num = (int) Math.round((Math.random()*9+1)*10000000);
			return sdf.format(date)+num;
		}
		/**
		 *- 获取年月日时间戳+9位uuid
		 * @return
		 */
		public static String getDateSimpleUUID() {
			UUID uuid = UUID.randomUUID();
		    String sdf = new SimpleDateFormat("yyyyMMdd").format(new Date());
		    String uuidNum = uuid.toString().replace("-", "").substring(0, 9);
		    uuidNum = sdf + uuidNum;
		    return uuidNum ;
		}
		
		 /** 
	     *- 对字符串md5加密(小写+字母) 
	     * ep:
	     *  "asd34" >> "35fc7aa9d2540259b05c682898db29c9"
	     * @param str 传入要加密的字符串 
	     * @return  MD5加密后的字符串 
	     */  
	    public static String MD5(String str) {
	        try {  
	            MessageDigest md = MessageDigest.getInstance("MD5");  
	            md.update(str.getBytes());
	            return new BigInteger(1, md.digest()).toString(16);  
	        } catch (Exception e) {  
	           e.printStackTrace();  
	           return null;  
	        }  
	    }  
		
		
		/**
		 * -字符串十进制转换成十六进制，以大写的形式返回来
		 * -ep：
		 *   "184467440" >> "AFEBFF0"
		 * @param ten
		 * @return
		 */
		public static String tenToSixteen(String ten) {
			if(StringUtils.isBlank(ten)) {
				return null;
			}
			long l = Long.valueOf(ten);
			String sixteen = Long.toHexString(l);
			return sixteen.toUpperCase();
	   }
		
		/**
		 * -字符串 十六进制转换成十进制
		 * -ep：
		 *   "AFEBFF0" >> "184467440"
		 * @param sixteen
		 * @return
		 */
		public static String sixteenToTen(String sixteen) {
			Long ten = Long.valueOf(sixteen,16);
			return ten.toString().toUpperCase();
		}
		
		
		/**
		 *- 将字符串数组转换成mysql的in数组
		 *-ep:
		 * ["1","2"] >> '1','2'
		 * @param strs
		 * @return
		 */
		public static String arrChangeIn(String[]strs){
			StringBuffer idsStr = new StringBuffer();
			for (int i = 0; i < strs.length; i++) {
				if (i > 0) {
					idsStr.append(",");
				}
				idsStr.append("'").append(strs[i]).append("'");
			}
			return idsStr.toString();
		}
		
		/**
		 * -数字字符串转换成int数组
		 * -ep:
		 *  "1,2,3" >> [1,2,3]
		 * @return
		 */
		public static int[] strToIntArr(String str) {
			String[] strS = str.split(",");
			int[] num = new int[strS.length];
			for(int i=0;i<strS.length;i++)
			{
				num[i]=Integer.valueOf(strS[i]);
			}
			return num;
		}
		
		/**
		 * -判断字符串是否为空
		 * @param str
		 * @return
		 */
		public static boolean checkStrEmpty(String str) {
			if(str == null||"".equals(str)) {
				return true;
			}else {
				return false;
			}
		}
		
		/**
		 * -使用正则表达式来判断字符串中是否包含字母
		 * @param cardNum
		 * @return
		 */
	    public static boolean judgeContainsLetter(String str) {
	    	String regex=".*[a-zA-Z]+.*";
		    Matcher m=Pattern.compile(regex).matcher(str);
		    return m.matches();
	    }
		
	    /**
	     * -测试main方法
	     * @param args
	     */
		public static void main(String[]args) {
			System.out.println(getyyyyMMddHHmmss());
			String s = "184467440";
			System.out.println(MD5("asd34"));
 
			System.out.println(sixteenToTen(tenToSixteen(s)));
		}
		
}
 
 
 